/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.meibang.meibanguserskillstype.web;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.meibang.meibanguserskillstype.entity.MeibangUserSkillsType;
import cn.apestar.modules.meibang.meibanguserskillstype.enums.MasterMJSLevelEnum;
import cn.apestar.modules.meibang.meibanguserskillstype.enums.MasterMRSLevelEnum;
import cn.apestar.modules.meibang.meibanguserskillstype.enums.MasterTypeEnum;
import cn.apestar.modules.meibang.meibanguserskillstype.service.MeibangUserSkillsTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 技师技能类型Controller
 *
 * @author yudw
 * @version 2017-05-03
 */
@Controller
@RequestMapping(value = "${adminPath}/meibanguserskillstype/meibangUserSkillsType")
public class MeibangUserSkillsTypeController extends BaseController {

    @Autowired
    private MeibangUserSkillsTypeService meibangUserSkillsTypeService;

    @ModelAttribute
    public MeibangUserSkillsType get(@RequestParam(required = false) String id) {
        MeibangUserSkillsType entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = meibangUserSkillsTypeService.get(id);
        }
        if (entity == null) {
            entity = new MeibangUserSkillsType();
        }
        return entity;
    }

    @RequestMapping(value = { "list", "" })
    public String list(MeibangUserSkillsType meibangUserSkillsType, HttpServletRequest request,
        HttpServletResponse response, Model model) {
        Page<MeibangUserSkillsType> page = meibangUserSkillsTypeService
            .findPage(new Page<MeibangUserSkillsType>(request, response), meibangUserSkillsType);
        //        setLevelForType(meibangUserSkillsType);
        model.addAttribute("page", page);
        this.setSkillAndLevel(model);
        return "modules/meibang/meibanguserskillstype/meibangUserSkillsTypeList";
    }

    private void setLevelForType(MeibangUserSkillsType meibangUserSkillsType) {
        if (StringUtils.isNotEmpty(meibangUserSkillsType.getType())) {
            if (meibangUserSkillsType.getType().equals(MasterTypeEnum.MJS_TYPE.getValue()))
                meibangUserSkillsType.setLevel(meibangUserSkillsType.getLevel1());
            if (meibangUserSkillsType.getType().equals(MasterTypeEnum.MRS_TYPE.getValue())) {
                meibangUserSkillsType.setLevel(meibangUserSkillsType.getLevel2());
            }
        }
    }

    @RequestMapping(value = "form")
    public String form(MeibangUserSkillsType meibangUserSkillsType, Model model) {
        model.addAttribute("meibangUserSkillsType", meibangUserSkillsType);
        this.setSkillAndLevel(model);
        //        if (StringUtils.isNotEmpty(meibangUserSkillsType.getType())) {
        //            if (meibangUserSkillsType.getType().equals(MasterTypeEnum.MJS.getValue()))
        //                meibangUserSkillsType.setLevel1(meibangUserSkillsType.getLevel());
        //            if (meibangUserSkillsType.getType().equals(MasterTypeEnum.MRS.getValue()))
        //                meibangUserSkillsType.setLevel2(meibangUserSkillsType.getLevel());
        //        }
        return "modules/meibang/meibanguserskillstype/meibangUserSkillsTypeForm";
    }

    private void setSkillAndLevel(Model model) {
        model.addAttribute("types", MasterTypeEnum.getMap());
        model.addAttribute("mjsLevel", MasterMJSLevelEnum.getList());
        model.addAttribute("mrsLevel", MasterMRSLevelEnum.getList());
    }

    @RequestMapping(value = "save")
    public String save(MeibangUserSkillsType meibangUserSkillsType, Model model,
        RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, meibangUserSkillsType)) {
            return form(meibangUserSkillsType, model);
        }
        //        setLevelForType(meibangUserSkillsType);
        meibangUserSkillsTypeService.save(meibangUserSkillsType);
        addMessage(redirectAttributes, "保存技师技能类型成功");
        return "redirect:" + Global.getAdminPath() + "/meibanguserskillstype/meibangUserSkillsType/?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(MeibangUserSkillsType meibangUserSkillsType, RedirectAttributes redirectAttributes) {
        meibangUserSkillsTypeService.delete(meibangUserSkillsType);
        addMessage(redirectAttributes, "删除技师技能类型成功");
        return "redirect:" + Global.getAdminPath() + "/meibanguserskillstype/meibangUserSkillsType/?repage";
    }

    @RequestMapping(value = "getType")
    public Object getType() {
        return getSuccessMessageObject("success", MasterTypeEnum.getList());
    }

    @RequestMapping(value = "getLevel")
    public Object getLevel(HttpServletRequest request, String typeId) {
        if (MasterTypeEnum.MJS_TYPE.getValue().equals(typeId)) {
            return getSuccessMessageObject("success", MasterMJSLevelEnum.getList());
        } else {
            return getSuccessMessageObject("success", MasterMRSLevelEnum.getList());
        }
    }

}